//
//  TwoPassengersACrisisController.m
//  Runone
//
//  Created by ekkaLyn on 2017/3/17.
//  Copyright © 2017年 runone. All rights reserved.
//

#import "TwoPassengersACrisisController.h"
#import "RequestHelper.h"
#import "MJExtension.h"
#import "CarListModel.h"
#import "AllLastestVehicleDynamic.h"
#import "HistoryViewController.h"
#import "CarCell.h"
#import "UIView+Message.h"
#import "PromptUtil.h"
#import "SVProgressHUD.h"
#import "GetBusDangeroursHistoryCountModel.h"
#import "BDVehicleCarListModel.h"
#import "AllLastestVehicleDynamic.h"
#import "SVProgressHUD.h"
#import "ParModel.h"

@interface TwoPassengersACrisisController ()
@property(nonatomic,strong)CarListModel *carListModel;
@property(nonatomic,strong)UIView *MessageView;
@property (strong, nonatomic) IBOutlet UITableView *carListTableView;
//@property(nonatomic,strong)NSTimer *timer;
@property (nonatomic, strong) NSArray *trajectoryArray;
@property (nonatomic, strong) NSMutableArray *forArray;
@property (nonatomic, assign) NSInteger currentPageIndex;
@property (nonatomic, strong) BDVehicleCarListModel *listModel;
@property (nonatomic, strong)  GetBusDangeroursHistoryCountModel *countModel;
@property (nonatomic, copy) NSString *carTypeStr; // 车辆类型的string

@end

@implementation TwoPassengersACrisisController
//-------------------------------------------

- (void)viewDidLoad {
    [super viewDidLoad];
    //    self.title=@"查询结果";
    if ([_carT integerValue] == 30 || [_carT integerValue] == 31 || [_carT integerValue] == 32) {
        _carTypeStr = @"危化品车";
    }else {
        _carTypeStr = @"营运客车";
    }
    
    _listModel = [[BDVehicleCarListModel alloc] init];
    _countModel  = [[GetBusDangeroursHistoryCountModel alloc] init];
//    _currentPageIndex = 0;
    _forArray = [[NSMutableArray alloc] init];
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    label.text = @"查询结果";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=label;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CarCell" bundle:nil] forCellReuseIdentifier:@"CarCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.CarDetailArray = [NSMutableArray array];
//    self.CarListArray   = [NSMutableArray array];
    [self requestCarList];
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}

//-(NSString *)jsonstr {
//    NSArray *arr=@[@{@"key":@"VehicleNo",@"value":@"鄂N08811"},@{@"key":@"BegineTime",@"value":@"2017-04-03 00:00:00"},@{@"key":@"EndTime",@"value":@"2017-04-04 00:00:00"}];
//    NSString *str=[arr mj_JSONString];
//    return str;
//}
-(NSString *)jsonstr {
    ParModel *param1 = [self paramWithKey:@"VehicleNo" Value:self.carNum];
    NSDictionary *dic1 = param1.mj_keyValues;
    ParModel *param2 = [self paramWithKey:@"BegineTime" Value:self.beginTime];
    NSDictionary *dic2 = param2.mj_keyValues;
    ParModel *param3 = [self paramWithKey:@"EndTime" Value:self.endTime];
    NSDictionary *dic3 = param3.mj_keyValues;
    NSArray *arr=@[dic1,dic2,dic3];
    NSString *str=[arr mj_JSONString];
    return str;
}

- (ParModel *) paramWithKey:(NSString *)key Value:(NSString *)value {
    ParModel *param = [[ParModel alloc] init];
    param.key   = key;
    param.value = value;
    return param;
}

-(void)requestCarList {
    //    __weak typeof(self) weakSelf = self;
    //NSLog(@"%@ %@ @%",self.carNum, self.beginTime, self.endTime);
    [SVProgressHUD showWithStatus:@"正在加载中"];
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetCarHistoryURL;
    getCurrentEventModel.DataValue=[self jsonstr];
    getCurrentEventModel.UpdatedTime=@"";
    getCurrentEventModel.SystemCode=@"GuangleHighway";
    getCurrentEventModel.MethodName=@"SearchHistoryTrajectory";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
       
        if ([requestOutginseng.Code isEqualToString:@"001"]) {
            _trajectoryArray = [BDVehicleCarListModel mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
            [self.tableView reloadData];
        }
         [SVProgressHUD dismiss];
    };
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.trajectoryArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CarCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CarCell" forIndexPath:indexPath];
    BDVehicleCarListModel *model = self.trajectoryArray[indexPath.row];
    cell.CarNumber.text  = _carNum;
    cell.BenginTime.text = model.BeginTime;
    cell.EndTime.text    = model.EndTime;
    cell.carTypeL.text   = _carTypeStr;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 116;
}


- (NSString *)forOneTrace:(NSString *)vehicleNO beginTime:(NSString *)bTimeStr endTime:(NSString *)eTimeStr {
    
    ParModel *param1 = [self paramWithKey:@"VehicleNo" Value:self.carNum];
    NSDictionary *dic1 = param1.mj_keyValues;
    ParModel *param2 = [self paramWithKey:@"BegineTime" Value:bTimeStr];
    NSDictionary *dic2 = param2.mj_keyValues;
    ParModel *param3 = [self paramWithKey:@"EndTime" Value:eTimeStr];
    NSDictionary *dic3 = param3.mj_keyValues;
    NSArray *arr=@[dic1,dic2,dic3];
    NSString *str=[arr mj_JSONString];
    return str;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _currentPageIndex = 0;
    [self.forArray removeAllObjects];
    [SVProgressHUD showWithStatus:@"正在加载中..."];
    _listModel = self.trajectoryArray[indexPath.row];
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetCarHistoryURL;
    getCurrentEventModel.DataValue   = [self forOneTrace:_carNum beginTime:_listModel.BeginTime endTime:_listModel.EndTime];
    getCurrentEventModel.UpdatedTime = [requestHelper getCurrentTime];
    getCurrentEventModel.SystemCode  = @"GuangleHighway";
    getCurrentEventModel.MethodName  = @"GetBusDangeroursHistoryCount";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        if ([requestOutginseng.Code isEqualToString:@"001"] ) {
            _countModel = [GetBusDangeroursHistoryCountModel mj_objectWithKeyValues:requestOutginseng.DataValue];
            if (_countModel.AllCount > 0) {
//                for (int i = 0; i < _countModel.PageSize; i ++) {
            
                    [self requestForTrac:_countModel andBT:_listModel.BeginTime ET:_listModel.EndTime];
               // }
            }else {
                [SVProgressHUD showErrorWithStatus:@"没有相关的轨迹信息"];
            }
        }else {
            [SVProgressHUD showSuccessWithStatus:@"请重新尝试"];
        }
    };
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}

- (void) pushToNextViewController {
    [SVProgressHUD dismiss];
//    if (self.forArray.count > 0 ) {
        HistoryViewController *vc = [[HistoryViewController alloc] init];
        vc.arr = self.forArray;
        [self.navigationController pushViewController:vc animated:YES];
//    }
}

-(NSString *)everyCountTrac:(NSString *)vehicleNO BegineTime:(NSString *)bTime EndTime:(NSString *)eTime PageIndex:(NSString *)pSize PageCount:(NSString *)pCount  {

    ParModel *param1 = [self paramWithKey:@"VehicleNo" Value:self.carNum];
    NSDictionary *dic1 = param1.mj_keyValues;
    ParModel *param2 = [self paramWithKey:@"BegineTime" Value:bTime];
    NSDictionary *dic2 = param2.mj_keyValues;
    ParModel *param3 = [self paramWithKey:@"EndTime" Value:eTime];
    NSDictionary *dic3 = param3.mj_keyValues;
    ParModel *param4 = [self paramWithKey:@"PageIndex" Value:pSize];
    NSDictionary *dic4 = param4.mj_keyValues;
    ParModel *param5 = [self paramWithKey:@"PageCount" Value:pCount];
    NSDictionary *dic5 = param5.mj_keyValues;
    NSArray *array = @[dic1, dic2, dic3, dic4, dic5];
    NSString *str  = array.mj_JSONString;
    return str;
}
//AllLastestVehicleDynamic
- (void) requestForTrac:(GetBusDangeroursHistoryCountModel *)countModel andBT:(NSString *)bTime ET:(NSString *)eTime {
    _currentPageIndex += 1;
    RequestHelper *requestHelper = [[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetCarHistoryURL;
    getCurrentEventModel.DataValue   = [self everyCountTrac:_carNum BegineTime:_listModel.BeginTime EndTime:_listModel.EndTime PageIndex:[NSString stringWithFormat:@"%ld",(long)_currentPageIndex] PageCount:[NSString stringWithFormat:@"%ld",(long)_countModel.PageCount]];
    getCurrentEventModel.UpdatedTime = [requestHelper getCurrentTime];
    getCurrentEventModel.SystemCode  = @"GuangleHighway";
    getCurrentEventModel.MethodName  = @"SearchHistoryTrajectoryDataByPage";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        if ([requestOutginseng.Code isEqualToString:@"001"]) {
            NSArray *array = [AllLastestVehicleDynamic mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
            if (array.count == 0) {
                [SVProgressHUD showSuccessWithStatus:@"没有查询到相关数据"];
                return ;
            }
            [self.forArray addObjectsFromArray:array];
            if (_currentPageIndex == _countModel.PageSize) {
                [SVProgressHUD dismiss];
                [self pushToNextViewController];
            } else {
            [self requestForTrac:_countModel andBT:_listModel.BeginTime ET:_listModel.EndTime ];
            }
        }else {
            [SVProgressHUD showErrorWithStatus:@"网络问题，请重新尝试"];
        }
    };
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}
@end
